package org.ands.client.test.oai;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ands.apps.PropertyHelper;
import org.ands.data.Contents;
import org.ands.data.ContentsRetrieverDataSetContainingContacts;
import org.ands.data.ContentsRetrieverOaiPmh;
import org.ands.data.IClientContentsRetriever;
import org.ands.input.InputHelperFactoryNodeList;
import org.ands.xml.NamespaceContextMCP;
import org.w3c.dom.NodeList;

public class ContentsRetrieverTestOAI implements IClientContentsRetriever {

	@Override
	public Map<Integer, List<Contents>> getXmlContentsFromClient(
			PropertyHelper propHelper) throws Exception {
		
		
		final String oaiPmhUri = propHelper.getProperty(PropertyHelper.TEST_URI, true);
		final String metadataPrefix = propHelper.getProperty(PropertyHelper.TEST_METADATA_IDENTIFIER, true);
		final String rootNodeName = propHelper.getProperty(PropertyHelper.TEST_ROOT_NODE_NAME, true);
		
		PropertyHelperTestOAI aimsPropertyHelper = new PropertyHelperTestOAI();
		Map<FileSubject, String> crosswalkFilenames = aimsPropertyHelper.getCrosswalkFilenames(propHelper);
		
		Map<Integer, List<Contents> > xmlContentsAllRegistryObjectsMap = new HashMap<Integer, List<Contents> >();
		
		ContentsRetrieverDataSetContainingContacts contentsRetrieverDataSetContainingContacts = new ContentsRetrieverDataSetContainingContacts();
		
		ContentsRetrieverOaiPmh contentsRetrieverOaiPmh = new ContentsRetrieverOaiPmh(
				oaiPmhUri, 
				"verb=ListRecords", 
				metadataPrefix, 
				rootNodeName, 
				true /*fetchnext*/); // Seek resumption token and continue
		
		while(contentsRetrieverOaiPmh.hasNodes()){
			NodeList nodeList = contentsRetrieverOaiPmh.getNodes();
		
			InputHelperFactoryNodeList inputHelperFactory = new InputHelperFactoryNodeList(nodeList, new NamespaceContextMCP());
			xmlContentsAllRegistryObjectsMap.putAll(contentsRetrieverDataSetContainingContacts.getXmlContentsFromSource(
					crosswalkFilenames,
					inputHelperFactory));
		}
		
		return xmlContentsAllRegistryObjectsMap;
	}
	
	@Override
	public String getOutFileFromConfig(PropertyHelper propertyHelper)
			throws Exception {
		
		return propertyHelper.getProperty("RIF_CS_XML_OUT_TEST_OAI", true);
		
	}
	

}
